home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000…tember: Reference Library / Dev.CD Sep 00 RL Disk 1.toast / mac / Technical Documentation / Develop / develop Issue 29 / develop Issue 29 code / Sending PostScript Files / SendPS Sample / sendps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-19  |  4.8 KB  |  184 lines  |  [TEXT/MPS ]

  1. #include <stdio.h>
  2. #include <Resources.h>
  3. #include <Memory.h>
  4. #include <CursorCtl.h>
  5. #include <ErrMgr.h>
  6. #include <TextUtils.h>
  7.  
  8. #include "pap.h"    // from PAPWorkStation.o package
  9. #include "CurrentPrinter.h"    // from the CurrentPrinter sample
  10.  
  11. ////////////////////////////////////////////////////////////////////////////////////
  12. // functions to bridge from old names to new
  13. ////////////////////////////////////////////////////////////////////////////////////
  14. short PAPRead(short refNum,Ptr ReadBuff,short *DataSize,short *eof,short *CompState)
  15. {
  16.     return WPAPRead(refNum, (void *)ReadBuff, DataSize, eof,(OSErr *)CompState);
  17. }
  18.  
  19. short PAPWrite(short refNum,Ptr WriteBuff,short DataSize,short eof,short *CompState)
  20. {
  21.     return WPAPWrite(refNum,(void *)WriteBuff, DataSize, eof,(OSErr *)CompState);
  22. }
  23.  
  24. short PAPClose(short refNum)
  25. {
  26.     return WPAPClose(refNum);
  27. }
  28.  
  29. static void ConvertEOLs(char *buffer,short length)
  30. {
  31.     short i;
  32.     if (length<0) {
  33.         DebugStr("\pFoolish Human! Bad length");
  34.         return;
  35.     }
  36.     if (!buffer) {
  37.         DebugStr("\pFoolish Human! Bad buffer");
  38.         return;
  39.     }
  40.     for(i=0;i<length;i++) {
  41.         if (buffer[i] == '\012')
  42.             buffer[i] = '\015';
  43.     }
  44. }
  45.  
  46. void displayStatus (struct PAPStatusRec *status)
  47. {
  48.     static Str255    oldStatus;
  49.     
  50.     if (EqualString(oldStatus,status->statusStr,false,false))
  51.         // nothing has changed - just return
  52.         return;
  53.     else {
  54.         // copy in the new status
  55.         BlockMove(status->statusStr,oldStatus,(status->statusStr[0])+1);
  56.         // and display it
  57.         // this assumes a printf that knows about pascal strings. If you
  58.         // don't have one, you'll need to convert the string yourself.
  59.         fprintf(stderr,"### %p\n",oldStatus);
  60.     }
  61. }
  62.  
  63. ////////////////////////////////////////////////////////////////////////////////////
  64. // here's where we do all the work
  65. ////////////////////////////////////////////////////////////////////////////////////
  66. int main(int argc, char **argv)
  67. {
  68.     Str255 lwname;
  69.     int fd, i;
  70.     char wbuffer[4096], rbuffer[4096];
  71.     struct PAPStatusRec status;
  72.     AddrBlock    printerAddress;
  73.     short rcnt, wcnt, reof;
  74.     short refNum, wstate, rstate;
  75.     char msg[256];
  76.  
  77.     if (argc != 2) {
  78.         fprintf(stderr, "### usage: %s filename\n", argv[0]);
  79.         exit(1);
  80.     }
  81.     fd = open(argv[1], 0);
  82.     if (fd < 0) {
  83.         fprintf(stderr, "### %s: can't open '%s' for reading\n", argv[0], argv[1]);
  84.         exit(1);
  85.     }
  86.     
  87.     if (GetCurrentPrinter(lwname) != noErr) exit(1);
  88.  
  89.     InitCursorCtl(NULL);
  90.     Show_Cursor(WATCH_CURSOR);
  91.  
  92.     i = PAPOpen(&refNum, (EntityName *) lwname, 8,&status, &wstate);
  93.     if (i) {
  94.         fprintf(stderr, "### %s: %s\n", argv[0], GetSysErrText(i, msg));
  95.         exit(1);
  96.     }
  97.     while (wstate > 0) {
  98.         // spin the cursor (which lets MPW check for cancels for us)
  99.         RotateCursor(32);
  100.         // oh, and display some status, too....
  101.         displayStatus(&status);
  102.     }
  103.     if (wstate) {
  104.         fprintf(stderr, "### %s: %s\n", argv[0], GetSysErrText(wstate, msg));
  105.         PAPClose(refNum);
  106.         WPAPUnload();
  107.         exit(1);
  108.     }
  109. //    DebugStr("\pOpened");
  110.  
  111.     // Issue a PAPRead so when the printer wants to talk to us, it can
  112.     rcnt = sizeof(rbuffer);
  113.     reof = 0;
  114.     PAPRead(refNum, (Ptr) rbuffer, &rcnt, &reof, &rstate);
  115. //    DebugStr("\pIssued that first read...");
  116.  
  117.     while ((i = read(fd, (Ptr) wbuffer, sizeof(wbuffer))) != 0) {
  118.         wcnt = i;
  119.         RotateCursor(32);
  120.         PAPWrite(refNum, (Ptr) wbuffer, wcnt, 0, &wstate);
  121.         while (wstate > 0) {
  122.             if (rstate == 0) {
  123.                 for (i = 0; i < rcnt; i++)
  124.                     if (rbuffer[i] != '\012') putchar(rbuffer[i]);
  125.                 fflush(stdout);
  126.                 rcnt = sizeof(rbuffer);
  127.                 reof = 0;
  128.                 RotateCursor(-32);
  129.                 PAPRead(refNum, (Ptr) rbuffer, &rcnt, &reof, &rstate);
  130.             } else if (rstate < 0) {
  131.                 fprintf(stderr, "### %s: %s\n", argv[0], GetSysErrText(rstate, msg));
  132.                 PAPClose(refNum);
  133.                 WPAPUnload();
  134.                 exit(1);
  135.             } else {
  136.                 // We're not doing anything else, check the status
  137.                 PAPStatus((EntityName *) lwname,&status,&printerAddress);
  138.                 displayStatus(&status);
  139.             }
  140.         }
  141.         if (wstate < 0) {
  142.             fprintf(stderr, "### %s: %s\n", argv[0], GetSysErrText(wstate, msg));
  143.             PAPClose(refNum);
  144.             WPAPUnload();
  145.             exit(1);
  146.         }
  147. //        DebugStr("\pSent a buffer");
  148.     }
  149.     RotateCursor(32);
  150.     PAPWrite(refNum, (Ptr) "", 0, 1, &wstate);
  151. //    DebugStr("\pSent EOF");
  152.     for(;;) {        // we check reof and break to exit
  153.         if (rstate == 0) {
  154.             char    foo[4096];
  155.  
  156.             memset(foo,0,4096);
  157.             strncpy(foo,rbuffer,rcnt);
  158.             ConvertEOLs(foo,rcnt);
  159.             printf("%s",foo);
  160.             fflush(stdout);
  161.             if (reof)
  162.                 // we received the end of file from the printer. We're done.
  163.                 break;
  164.             rcnt = sizeof(rbuffer);
  165.             reof = 0;
  166.             RotateCursor(-32);
  167.             PAPRead(refNum, (Ptr) rbuffer, &rcnt, &reof, &rstate);
  168.         } else if (rstate < 0) {
  169.             // got an error
  170.             fprintf(stderr, "### %s: %s\n", argv[0], GetSysErrText(rstate, msg));
  171.             PAPClose(refNum);
  172.             WPAPUnload();
  173.             exit(1);
  174.         } else {
  175.             // We're not done yet, so check the status
  176.             PAPStatus((EntityName *) lwname,&status,&printerAddress);
  177.             displayStatus(&status);
  178.         }
  179.     }
  180.     PAPClose(refNum);
  181.     WPAPUnload();
  182. //    DebugStr("\pClosed normally");
  183. }
  184.